# -*- coding:utf-8 -*-
__author__ = 'CYY'
from constant_util import logger, __mysql_config_file, Properties
import pymysql


def get_mysql_con():
    config = Properties(__mysql_config_file).get_properties()
    return pymysql.connect(
        host=config['mysql.host'],
        port=int(config['mysql.port']),
        user=config['mysql.user'],
        password=config['mysql.pass'],
        db='azkaban',
        charset='utf8',
        cursorclass=pymysql.cursors.DictCursor
    )


def exec_mysql_sql(sql):
    if sql:
        con = get_mysql_con()
        try:
            with con.cursor() as cur:
                logger.debug('执行sql：%s', sql)
                cur.execute(sql.strip())
                con.commit()
                r = cur.fetchall()
                logger.debug("结果：%s", r)
                return r
        except:
            con.rollback()
        finally:
            con.close()
